<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="main">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Hojoki Client</title>
	<link type='text/css' rel='stylesheet' href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' />
	<link type="text/css" rel="stylesheet" href="css/jquery-ui-aristo/jquery-ui-1.8.7.custom.css" />
	<link type="text/css" rel="stylesheet" href="css/style.css" />

	<script type="text/javascript" src="js/jquery/jquery-1.6.min.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/jquery/jquery-ui.min.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/hojokiclient/ActivityManager.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/hojokiclient/Activity.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/hojokiclient/User.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/AIRAliases.js" charset="utf-8"></script>
	<script type="text/javascript" src="js/webtoolkit.base64.js" charset="utf-8"></script>
	
	<!--<script type="text/javascript" src="js/AIRIntrospector.js" charset="utf-8"></script>-->
</head>
<body>

<script type="text/javascript" charset="utf-8">
	var DEBUG_OUTPUT = true; // true | false	
	debug("Welcome to Hojoki-Debugging :)");		
	
	var curScreen = air.Screen.screens[0]; // boundaries for main window	
	
	/* --- pointer to main/login window + center position --- */
	var mainWindow = window.nativeWindow; 		
	mainWindow.x = ( curScreen.bounds.width - mainWindow.width ) / 2;
	mainWindow.y = ( curScreen.bounds.height - mainWindow.height ) / 2;	
	
	/* --- initialize user --- */
	var user = new User();	
	
	/* --- setup main window --- */
	jQuery(document).ready(function() {
		// login button jquery ui style
		jQuery('button[name="login"]').button();		

		/* --- submit login form --- */
		jQuery('.login-field').keyup(function(evt) {
			if(evt.keyCode == '13') checkLoginInput();
		});
		jQuery('button[name="login"]').click(function() { checkLoginInput(); });
		
		// fade in main window
		jQuery('body').fadeIn(1000);	
	});
	
	
	jQuery(window).load(function() {
		window.htmlLoader.authenticate = false; // disable AIR's authentication handling, which could cause login dialogs
	});
	

	/* --- establish sandbox bridges to allow communication between main.html and myHojokiApp --- */
	function setupSandboxBridge() {
		/* --- parent sandbox bridge --- */
		exposed = {};
		exposed.debug = function(msg) { debug(msg); };
		exposed.checkLogin = function(status) { user.checkLogin(status); };
		exposed.pushActivity = function(activityHTML) { user.activityManager.pushActivity(activityHTML); };
		exposed.ready = function() {
			/* --- child sandbox bridge --- */
			childExposed = document.getElementById("connector").contentWindow.childSandboxBridge; 
			userRemembered(); // check if user credentials are saved locally 
		};
		document.getElementById("connector").contentWindow.parentSandboxBridge = exposed;
	}
	
	
	function userRemembered() {
		if (user.email != "") // user credentials are saved locally, try to login without showing the login form 
			user.doLogin(); 
		else { // show login form
			jQuery("#loginForm").show();
			jQuery("#loading").hide();
		}
	}
	
	
	/* --- check form for basic mistakes (empty, no email structure, ..) --- */
	function checkLoginInput() {
		user.email = jQuery('input[name="email"]').val();
		user.pwd = jQuery('input[name="pwd"]').val();
		var error = new Array(false,false); // error in email index 0, error in pwd index 1
		
		/* --- check for input errors --- */
		if(user.email == "") error[0] = true;
		if(user.email.indexOf("@") < 0 || user.email.indexOf(".") < 0) error[0] = true;
		if(user.pwd == "") error[1] = true;
		
		/* --- show errors --- */
		if(error[0]) showError('input[name="email"]');
		if(error[1]) showError('input[name="pwd"]');
		
		/* --- login if there are no errors --- */
		if(!error[0] && !error[1]) user.doLogin();
	}
	
	
	/* --- indicate an error with flashing up the background color of the target --- */
	function showError(target) {
		jQuery(target).stop().animate({backgroundColor:"#ff9999"},200).animate({backgroundColor:"#ffffff"},1500);
	}

	
	/* --- create the system tray icon, if supported (the Mac dock icon takes care of itself) --- */
	function createSysTray() {		
		var iconLoadComplete = function(event){
			if(air.NativeApplication.supportsSystemTrayIcon){						
					air.NativeApplication.nativeApplication.icon.bitmaps = new Array(event.target.content.bitmapData);
					air.NativeApplication.nativeApplication.icon.tooltip = "Hojoki Client";
					air.NativeApplication.nativeApplication.icon.menu = new air.NativeMenu();
					
					/* --- About --- */
					var aboutCommand = new air.NativeMenuItem("About");
					air.NativeApplication.nativeApplication.icon.menu.addItem(aboutCommand);
					aboutCommand.addEventListener(air.Event.SELECT,function(event){
						var curScreen = air.Screen.screens[0];
						
						var options = new air.NativeWindowInitOptions();		
						options.type = air.NativeWindowType.LIGHTWEIGHT;
						options.systemChrome = air.NativeWindowSystemChrome.NONE;
						options.transparent = true;
						
						var loader = air.HTMLLoader.createRootWindow( false, options, false );
						
						var bounds = new air.Rectangle();		
						bounds.x = ( curScreen.bounds.width - 430 ) / 2;
						bounds.y = ( curScreen.bounds.height - 325 ) / 2;
						bounds.width = 430;
						bounds.height = 325;
				
						loader.window.nativeWindow.bounds = bounds;
						loader.window.nativeWindow.alwaysInFront = true;
						loader.window.nativeWindow.visible = true;
						loader.load( new air.URLRequest("about.html"));
					});
					
					/* --- Logout --- */
					var logoutCommand = new air.NativeMenuItem("Logout");
					air.NativeApplication.nativeApplication.icon.menu.addItem(logoutCommand);
					logoutCommand.addEventListener(air.Event.SELECT,function(event){
						user.doLogout();
					});
					
					/* --- Exit --- */
					var exitCommand = new air.NativeMenuItem("Exit");
					air.NativeApplication.nativeApplication.icon.menu.addItem(exitCommand);
					exitCommand.addEventListener(air.Event.SELECT,function(event){
						closeApp();
					});
			};
		}		
		var iconLoader = new runtime.flash.display.Loader();
		iconLoader.contentLoaderInfo.addEventListener(air.Event.COMPLETE,iconLoadComplete);
		iconLoader.load(new air.URLRequest("img/Hojoki_16.png"));
	}
	
	
	/* --- close the entire application --- */
	function closeApp() {
		if(user.logged) childExposed.doLogout();
		jQuery('body').fadeOut(300, function() { air.NativeApplication.nativeApplication.exit(); });	
	}
	

	/* --- print debugging outputs to the console --- */
	function debug(msg) { if(DEBUG_OUTPUT) air.trace(msg); }
	
</script>

<div id="wrapper">
	<div id="loading"></div>
	<div id="loginForm">
		<input class="login-field" name="email" type="email" placeholder="user@googlemail.com" value="" />		
		<input class="login-field" name="pwd" type="password" placeholder="Password" value="" />		
		<div id="remember"><input name="remember" type="checkbox" checked="checked" /> <p>Keep me logged in</p></div>		
		<button name="login" type="button">Login</button>	
	</div>
	
	<button name="close" type="button" onClick="closeApp()"></button>
	
    <!-- 
	files loaded from documentRoot directory are treated as if they would actually have been loaded from sandboxRoot URL
	http://help.adobe.com/en_US/air/html/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7f08.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7f07	
	-->
    <iframe id="connector"
		src="http://alpha.hojoki.com/myHojokiApp/v1/?app=http://hojoki-desktop-client.googlecode.com/svn/trunk/api/getActivities.js" 
		sandboxRoot="http://www.example.com/" 
		documentRoot="app:/" 
		allowcrossDomainxhr="true" 
		ondominitialize="setupSandboxBridge()">
    </iframe>
</div>

</body>
</html>
